﻿#if WINDOWS
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;
using Nuclex.Input;

namespace FoodWar.Episode1.Controller
{
    public struct PCGamePadState
    {
        public bool Up, Down, Left, Right, Attack, Block, Reset;
    }

    public struct PCGamePadsInfo
    {
        public ExtendedPlayerIndex index;
        public string Name;
    }

    public class PCGamePad
    {
        //public GameFoodWar game;

        public static int GetNumberOfPCGamePadsConnected(InputManager input)
        {
            int count = 0;
            if (input.GetGamePad(ExtendedPlayerIndex.Five).IsAttached) count++;
            if (input.GetGamePad(ExtendedPlayerIndex.Six).IsAttached) count++;
            if (input.GetGamePad(ExtendedPlayerIndex.Seven).IsAttached) count++;
            if (input.GetGamePad(ExtendedPlayerIndex.Eight).IsAttached) count++;
            return count;
        }

        public static List<PCGamePadsInfo> GetPCGamePadsInfos(InputManager input)
        {
            List<PCGamePadsInfo> Result = new List<PCGamePadsInfo>();
            if (input.GetGamePad(ExtendedPlayerIndex.Five).IsAttached)
            {
                PCGamePadsInfo info = new PCGamePadsInfo();
                info.index = ExtendedPlayerIndex.Five;
                info.Name = input.GetGamePad(ExtendedPlayerIndex.Five).Name;
                Result.Add(info);
            }
            if (input.GetGamePad(ExtendedPlayerIndex.Six).IsAttached)
            {
                PCGamePadsInfo info = new PCGamePadsInfo();
                info.index = ExtendedPlayerIndex.Six;
                info.Name = input.GetGamePad(ExtendedPlayerIndex.Six).Name;
                Result.Add(info);
            }
            if (input.GetGamePad(ExtendedPlayerIndex.Seven).IsAttached)
            {
                PCGamePadsInfo info = new PCGamePadsInfo();
                info.index = ExtendedPlayerIndex.Seven;
                info.Name = input.GetGamePad(ExtendedPlayerIndex.Seven).Name;
                Result.Add(info);
            }
            if (input.GetGamePad(ExtendedPlayerIndex.Eight).IsAttached)
            {
                PCGamePadsInfo info = new PCGamePadsInfo();
                info.index = ExtendedPlayerIndex.Eight;
                info.Name = input.GetGamePad(ExtendedPlayerIndex.Eight).Name;
                Result.Add(info);
            }
            return Result;
        }

        public static bool IsAnyButtonPressed(InputManager input, ExtendedPlayerIndex index)
        {
            if (input.GetGamePad(index).GetExtendedState().GetButton(11) == ButtonState.Pressed) return true;
            if (input.GetGamePad(index).GetExtendedState().GetButton(10) == ButtonState.Pressed) return true;
            if (input.GetGamePad(index).GetExtendedState().GetButton(9) == ButtonState.Pressed) return true;
            if (input.GetGamePad(index).GetExtendedState().GetButton(8) == ButtonState.Pressed) return true;
            if (input.GetGamePad(index).GetExtendedState().GetButton(7) == ButtonState.Pressed) return true;
            if (input.GetGamePad(index).GetExtendedState().GetButton(6) == ButtonState.Pressed) return true;
            if (input.GetGamePad(index).GetExtendedState().GetButton(5) == ButtonState.Pressed) return true;
            if (input.GetGamePad(index).GetExtendedState().GetButton(4) == ButtonState.Pressed) return true;
            if (input.GetGamePad(index).GetExtendedState().GetButton(3) == ButtonState.Pressed) return true;
            if (input.GetGamePad(index).GetExtendedState().GetButton(2) == ButtonState.Pressed) return true;
            if (input.GetGamePad(index).GetExtendedState().GetButton(1) == ButtonState.Pressed) return true;
            if (input.GetGamePad(index).GetExtendedState().GetButton(0) == ButtonState.Pressed) return true;
            return false;
        }

        public static bool IsAnyButtonAtAnyGamePadPressed(InputManager input)
        {
            if (IsAnyButtonPressed(input, ExtendedPlayerIndex.Five)) return true;
            if (IsAnyButtonPressed(input, ExtendedPlayerIndex.Six)) return true;
            if (IsAnyButtonPressed(input, ExtendedPlayerIndex.Seven)) return true;
            if (IsAnyButtonPressed(input, ExtendedPlayerIndex.Eight)) return true;
            return false;
        }

        public static ExtendedPlayerIndex GetNextPCGamepad(InputManager input)
        {
            if (IsAnyButtonPressed(input, ExtendedPlayerIndex.Five)) return ExtendedPlayerIndex.Five;
            if (IsAnyButtonPressed(input, ExtendedPlayerIndex.Six)) return ExtendedPlayerIndex.Six;
            if (IsAnyButtonPressed(input, ExtendedPlayerIndex.Seven)) return ExtendedPlayerIndex.Seven;
            if (IsAnyButtonPressed(input, ExtendedPlayerIndex.Eight)) return ExtendedPlayerIndex.Eight;
            return ExtendedPlayerIndex.Five;
        }

        public static bool AnyPCGamePadUpPressed(InputManager input)
        {
            if (GetState(input, ExtendedPlayerIndex.Five).Up) return true;
            if (GetState(input, ExtendedPlayerIndex.Six).Up) return true;
            if (GetState(input, ExtendedPlayerIndex.Seven).Up) return true;
            if (GetState(input, ExtendedPlayerIndex.Eight).Up) return true;
            return false;
        }

        public static bool AnyPCGamePadDownPressed(InputManager input)
        {
            if (GetState(input, ExtendedPlayerIndex.Five).Down) return true;
            if (GetState(input, ExtendedPlayerIndex.Six).Down) return true;
            if (GetState(input, ExtendedPlayerIndex.Seven).Down) return true;
            if (GetState(input, ExtendedPlayerIndex.Eight).Down) return true;
            return false;
        }

        public static bool AnyPCGamePadLeftPressed(InputManager input)
        {
            if (GetState(input, ExtendedPlayerIndex.Five).Left) return true;
            if (GetState(input, ExtendedPlayerIndex.Six).Left) return true;
            if (GetState(input, ExtendedPlayerIndex.Seven).Left) return true;
            if (GetState(input, ExtendedPlayerIndex.Eight).Left) return true;
            return false;
        }

        public static bool AnyPCGamePadRightPressed(InputManager input)
        {
            if (GetState(input, ExtendedPlayerIndex.Five).Right) return true;
            if (GetState(input, ExtendedPlayerIndex.Six).Right) return true;
            if (GetState(input, ExtendedPlayerIndex.Seven).Right) return true;
            if (GetState(input, ExtendedPlayerIndex.Eight).Right) return true;
            return false;
        }

        public static bool AnyPCGamePadAttackPressed(InputManager input)
        {
            if (GetState(input, ExtendedPlayerIndex.Five).Attack) return true;
            if (GetState(input, ExtendedPlayerIndex.Six).Attack) return true;
            if (GetState(input, ExtendedPlayerIndex.Seven).Attack) return true;
            if (GetState(input, ExtendedPlayerIndex.Eight).Attack) return true;
            return false;
        }

        public static bool AnyPCGamePadBlockPressed(InputManager input)
        {
            if (GetState(input, ExtendedPlayerIndex.Five).Block) return true;
            if (GetState(input, ExtendedPlayerIndex.Six).Block) return true;
            if (GetState(input, ExtendedPlayerIndex.Seven).Block) return true;
            if (GetState(input, ExtendedPlayerIndex.Eight).Block) return true;
            return false;
        }

        public static PCGamePadState AnyPCGamePadGetState(InputManager input)
        {
            PCGamePadState result = new PCGamePadState();
            if (AnyPCGamePadUpPressed(input)) result.Up = true;
            if (AnyPCGamePadDownPressed(input)) result.Down = true;
            if (AnyPCGamePadLeftPressed(input)) result.Left = true;
            if (AnyPCGamePadRightPressed(input)) result.Right = true;
            if (AnyPCGamePadAttackPressed(input)) result.Attack = true;
            if (AnyPCGamePadBlockPressed(input)) result.Block = true;
            return result;
        }

        public static PCGamePadState GetState(InputManager input, ExtendedPlayerIndex Index)
        {
            PCGamePadState result = new PCGamePadState();
            if (input.GetGamePad(Index).GetExtendedState().GetButton(8) == ButtonState.Pressed) result.Reset = true;
            if (input.GetGamePad(Index).GetExtendedState().GetButton(2) == ButtonState.Pressed) result.Attack = true;
            if (input.GetGamePad(Index).GetExtendedState().GetButton(3) == ButtonState.Pressed) result.Block = true;
            int direction = input.GetGamePad(Index).GetExtendedState().Pov1;
            switch (direction)
            {
                case 00:
                    result.Up = true;
                    break;
                case 4500:
                    result.Up = true;
                    result.Right = true;
                    break;
                case 9000:
                    result.Right = true;
                    break;
                case 13500:
                    result.Right = true;
                    result.Down = true;
                    break;
                case 18000:
                    result.Down = true;
                    break;
                case 22500:
                    result.Left = true;
                    result.Down = true;
                    break;
                case 27000:
                    result.Left = true;
                    break;
                case 31500:
                    result.Left = true;
                    result.Up = true;
                    break;
                default:
                    break;
            }
            return result;
        }
    }
}

#endif